package com.pactera.controller.common.web;

import java.util.Collections;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.boot.autoconfigure.web.BasicErrorController;
import org.springframework.boot.autoconfigure.web.ErrorAttributes;
import org.springframework.boot.autoconfigure.web.ErrorProperties;
import org.springframework.http.HttpStatus;
import org.springframework.http.MediaType;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

import springfox.documentation.annotations.ApiIgnore;

/**
 * 全局错误页面控制类
 * @author lee
 */
@ApiIgnore
@Controller
@RequestMapping(value = "/error")
public class GlobalErrorController extends BasicErrorController {
	
    Logger logger =LoggerFactory.getLogger(GlobalErrorController.class);

    public GlobalErrorController(ErrorAttributes errorAttributes, ErrorProperties errorProperties) {
        super(errorAttributes, errorProperties);
    }

    @Override
    public ModelAndView errorHtml(HttpServletRequest request, HttpServletResponse response) {
        HttpStatus httpStatus = getStatus(request);
        response.setStatus(httpStatus.value());
        Map<String, Object> model = Collections.unmodifiableMap(getErrorAttributes(request,isIncludeStackTrace(request,MediaType.TEXT_HTML)));
        ModelAndView modelAndView = resolveErrorView(request, response, httpStatus, model);
        if(modelAndView==null) {
            if(httpStatus.equals(HttpStatus.NOT_FOUND)){
            	modelAndView = new ModelAndView("error/404", model);
            }else if(httpStatus.equals(HttpStatus.INTERNAL_SERVER_ERROR)){
            	modelAndView = new ModelAndView("error/500", model);
            }
        }
        return modelAndView;
    }
}